package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.modules.app.entity.AppModi;
import com.zjmzxfzhl.modules.app.entity.AppProj;
import com.zjmzxfzhl.modules.app.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ListenerServiceImpl implements ListenerService {

    @Autowired
    private AppModiService appModiService;
    @Autowired
    private AppProjService appProjService;
    @Autowired
    private AppDocService appDocService;
    @Autowired
    private AppProdVerService appProdVerService;
    @Autowired
    private AppLogService appLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modiLeaveStatus(String modiId) {
        AppModi appModi=appModiService.getById(modiId);
        if(appModi==null){
            return;
        }
        switch (appModi.getModiType()) {
            case "00": //项目变更
                AppProj appProj = appProjService.getById(appModi.getModiTypeId());
                if (appProj == null) {
                    return;
                }
                switch (appModi.getModiSubtype()) {
                    case "00": //普通变更
                        appProj.setIsModi("0");
                        appProj.setRevise("1");
                        break;
                    case "01": //中止变更
                        appProj.setIsModi("0");
                        appProj.setStatus("04");
                        break;
                    case "02": //重启变更
                        appProj.setIsModi("0");
                        appProj.setStatus("01");
                        break;
                    case "03": //终止变更
                        appProj.setIsModi("1");
                        appProj.setStatus("03");
                        break;
                }
                appProjService.updateById(appProj);
                break;
            case "01": //任务变更

                break;
            case "02": //产品变更

                break;
            case "03": //文档变更

                break;
            case "04": //合同变更

                break;
            default: return;
        }
        appModiService.reviewed(appModi.getModiId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void docLeaveStatus(String docId) {
        appDocService.reviewed(docId);
    }

    @Override
    public void prodVerLeaveStatus(String verId) {
        appProdVerService.reviewed(verId);
    }

    @Override
    public void modiProcessRecord(String modiId, String user) {
        AppModi appModi=appModiService.getById(modiId);
        if(appModi==null){
            return;
        }
        switch (appModi.getModiType()) {
            case "00": //项目变更同意
                appLogService.addListenerLog("同意项目变更","06",modiId,user);
                break;
            case "01": //任务变更同意

                break;
            case "02": //产品变更同意

                break;
            case "03": //文档变更同意

                break;
            case "04": //合同变更同意

                break;
            default: return;
        }
    }

    @Override
    public void docProcessRecord(String docId, String user) {
        appLogService.addListenerLog("审批同意","04",docId,user);
    }

    @Override
    public void prodVerProcessRecord(String verId, String user) {
        appLogService.addListenerLog("审批同意","07",verId,user);
    }

    @Override
    public void modiRejectRecord(String modiId, String user) {
        AppModi appModi=appModiService.getById(modiId);
        if(appModi==null){
            return;
        }
        switch (appModi.getModiType()) {
            case "00": //项目变更驳回
                appModiService.reject(modiId);
                appLogService.addListenerLog("驳回项目变更","06",modiId,user);
                break;
            case "01": //任务变更驳回

                break;
            case "02": //产品变更驳回

                break;
            case "03": //文档变更驳回

                break;
            case "04": //合同变更驳回

                break;
            default: return;
        }
    }

    @Override
    public void docRejectRecord(String docId, String user) {
        appDocService.reject(docId);
        appLogService.addListenerLog("驳回审批","04",docId,user);
    }

    @Override
    public void prodVerRejectRecord(String verId, String user) {
        appProdVerService.reject(verId);
        appLogService.addListenerLog("驳回审批","07",verId,user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modiStopStatus(String modiId) {
        AppModi appModi=appModiService.getById(modiId);
        if(appModi==null){
            return;
        }
        switch (appModi.getModiType()) {
            case "00": //项目变更
                AppProj appProj = appProjService.getById(appModi.getModiTypeId());
                if (appProj == null) {
                    return;
                }
                appProj.setIsModi("0");
                appProjService.updateById(appProj);
                break;
            case "01": //任务变更

                break;
            case "02": //产品变更

                break;
            case "03": //文档变更

                break;
            case "04": //合同变更

                break;
            default: return;
        }
        appModiService.close(appModi.getModiId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void docStopStatus(String docId) {

    }

    @Override
    public void prodVerStopStatus(String verId) {

    }

}

